package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQueryObject;
import cn.wolfcode.luowowo.comment.query.TravelCommentQueryObject;
import cn.wolfcode.luowowo.comment.repository.StrategyCommentMongoRepository;
import cn.wolfcode.luowowo.comment.repository.TravelCommentMongoRepository;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentMongoRepository repository;

    //要返回comment对象, 因为页面需要评论对象的文档id
    public TravelComment save(TravelComment comment) {
        //填充评论时间
        comment.setCreateTime(new Date());

        //如果是回复型评论
        if (comment.getType() == TravelComment.TRAVEL_COMMENT_TYPE) {
            //拿着前端封装到comment里面的refComment.id, 去数据库查找这个id对应的评论
            TravelComment ref = repository.findById(comment.getRefComment().getId()).get();
            //把这个原评论设置进当前要新增的评论当做引用
            comment.setRefComment(ref);
        }
        repository.save(comment);
        return comment;
    }

    public List<TravelComment> listByTravelId(Long travelId) {
        return repository.findByTravelId(travelId);
    }


}
